package com.infosys.newSimpleInvertedIndex.helper;
import com.infosys.newSimpleInvertedIndex.entities.Relative;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class RelativeLuceneHelper extends BaseLuceneHelper<Relative> {
    @Override
    public void constructIndex(List<Relative> relatives, boolean erase) throws IOException {
        // get path
        File indexFile = new File(System.getProperty("user.dir") + indexPath + "/relativeIndex");
        Directory indexDirectory = FSDirectory.open(Paths.get(indexFile.getAbsolutePath()));
        // construct writer
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
        IndexWriter indexWriter = new IndexWriter(indexDirectory, indexWriterConfig);
        if (erase){
            indexWriter.deleteAll();
        }
        for (Relative relative : relatives) {
            org.apache.lucene.document.Document luceneDocument = new org.apache.lucene.document.Document();
            // add all fields in employee
            luceneDocument.add(new StringField("id", relative.getId(), Field.Store.YES));
            luceneDocument.add(new StringField("employee_id", relative.getEmployeeId(), Field.Store.YES));
            luceneDocument.add(new StringField("name", relative.getName(), Field.Store.YES));
            luceneDocument.add(new StringField("jobTitle", relative.getJobTitle(), Field.Store.YES));
            luceneDocument.add(new StringField("politicalTitle", relative.getPoliticalTitle(), Field.Store.YES));
            luceneDocument.add(new StringField("type", relative.getType(), Field.Store.YES));
            indexWriter.addDocument(luceneDocument);
        }
        indexWriter.close();
        System.out.println("Index Update Complete");
    }

    @Override
    public List<Relative> queryIndex(String field, String queryText) throws IOException{
        File indexFile = new File(System.getProperty("user.dir") + indexPath + "/relativeIndex");
        Directory directory = FSDirectory.open(Paths.get(indexFile.getAbsolutePath()));

        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        // create QueryTerm for the indexing query
        Query luceneQuery = new TermQuery(new Term(field, queryText));
        TopDocs topDocs = indexSearcher.search(luceneQuery, Integer.MAX_VALUE);

        int totalMatches = (int)topDocs.totalHits.value;
        // document scores
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        List<Relative> returnRelatives = new ArrayList<>();

        // traver the interesting
        for (ScoreDoc scoreDoc : scoreDocs) {
            org.apache.lucene.document.Document document = indexSearcher.doc(scoreDoc.doc);
            Relative relative = new Relative();
            relative.setId(document.get("id"));
            relative.setEmployeeId(document.get("employee_id"));
            relative.setName(document.get("name"));
            relative.setJobTitle(document.get("jobTitle"));
            relative.setPoliticalTitle(document.get("politicalTitle"));
            relative.setType(document.get("type"));
            returnRelatives.add(relative);
        }
        indexReader.close();
        return returnRelatives;
    }
}

